<?php
/**
 * +----------------------------------------------------------------------
 * | summer framework
 * +----------------------------------------------------------------------
 * | Copyright (c) 2020 https://github.com/suframe/think-admin All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: summer <806115620@qq.com>  2020/3/30 14:22
 * +----------------------------------------------------------------------
 */
declare (strict_types=1);

namespace app\mall\model\traits;

use app\mall\model\MallUser;
use think\model\relation\BelongsTo;

/**
 * trait UserTrait
 * @package app\mall\model\traits
 * @mixin \think\Model
 * @property integer $uid
 */
trait UserTrait
{

    /**
     * 获取会员
     * @return BelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(MallUser::class, 'uid',
            'id')->cache('modelBelongsUser' . $this->uid, 60);
    }

    /**
     * 获取会员名称
     * @return string
     */
    public function getUserNicknameAttr(): string
    {
        return $this->getUserInfo('nickname');
    }

    /**
     * 获取会员头像
     * @return string
     */
    public function getUserAvatarAttr()
    {
        return $this->getUserInfo('avatar');
    }

    /**
     * 获取会员头像
     * @return string
     */
    public function getUserPhoneAttr()
    {
        return $this->getUserInfo('phone');
    }

    private function getUserInfo($key)
    {
        $model = $this->user();
        if (!$model) {
            return '';
        }
        return $model->value($key);
    }

}